import * as vscode from 'vscode';
import * as path from 'path';
import * as os from 'os';
import * as fs from 'fs';

/**
 * 面板项类型
 */
export class PanelItem extends vscode.TreeItem {
    constructor(
        public readonly label: string,
        public readonly collapsibleState: vscode.TreeItemCollapsibleState,
        public readonly command?: vscode.Command,
        public readonly iconPath?: vscode.ThemeIcon,
        public readonly description?: string
    ) {
        super(label, collapsibleState);
        this.command = command;
        this.iconPath = iconPath;
        this.description = description;
    }
}

/**
 * 面板数据提供者
 */
export class PanelProvider implements vscode.TreeDataProvider<PanelItem> {
    private _onDidChangeTreeData: vscode.EventEmitter<PanelItem | undefined | null | void> = new vscode.EventEmitter<PanelItem | undefined | null | void>();
    readonly onDidChangeTreeData: vscode.Event<PanelItem | undefined | null | void> = this._onDidChangeTreeData.event;

    constructor() { }

    refresh(): void {
        this._onDidChangeTreeData.fire();
    }

    getTreeItem(element: PanelItem): vscode.TreeItem {
        return element;
    }

    getChildren(element?: PanelItem): Thenable<PanelItem[]> {
        if (!element) {
            // 根节点
            return Promise.resolve(this.getRootItems());
        }
        return Promise.resolve([]);
    }

    private getRootItems(): PanelItem[] {
        const items: PanelItem[] = [];

        // 打开控制面板按钮
        items.push(new PanelItem(
            '🎨 打开控制面板',
            vscode.TreeItemCollapsibleState.None,
            {
                command: 'cursor-restore.openPanel',
                title: '打开控制面板'
            },
            new vscode.ThemeIcon('window'),
            '点击打开漂亮的控制面板'
        ));

        // 状态信息组
        const statusInfo = this.getStatusInfo();
        items.push(new PanelItem(
            '状态信息',
            vscode.TreeItemCollapsibleState.Expanded,
            undefined,
            new vscode.ThemeIcon('info'),
            statusInfo
        ));

        // 功能按钮组
        items.push(new PanelItem(
            '输入激活码',
            vscode.TreeItemCollapsibleState.None,
            {
                command: 'cursor-restore.inputCard',
                title: '输入激活码'
            },
            new vscode.ThemeIcon('key')
        ));

        items.push(new PanelItem(
            '输入Token',
            vscode.TreeItemCollapsibleState.None,
            {
                command: 'cursor-restore.inputToken',
                title: '输入Token'
            },
            new vscode.ThemeIcon('lock')
        ));

        items.push(new PanelItem(
            '一键特权',
            vscode.TreeItemCollapsibleState.None,
            {
                command: 'cursor-restore.privilege',
                title: '一键特权'
            },
            new vscode.ThemeIcon('star')
        ));

        items.push(new PanelItem(
            '一键续杯',
            vscode.TreeItemCollapsibleState.None,
            {
                command: 'cursor-restore.renewal',
                title: '一键续杯'
            },
            new vscode.ThemeIcon('sync')
        ));

        items.push(new PanelItem(
            '查看状态',
            vscode.TreeItemCollapsibleState.None,
            {
                command: 'cursor-restore.showStatus',
                title: '查看状态'
            },
            new vscode.ThemeIcon('info')
        ));

        return items;
    }

    private getStatusInfo(): string {
        try {
            const configPath = this.getConfigPath();
            if (fs.existsSync(configPath)) {
                const config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
                return `激活码: ${config.card_code ? '已设置' : '未设置'} | Token: ${config.token ? '已设置' : '未设置'}`;
            }
        } catch (error) {
            // 忽略错误
        }
        return '未配置';
    }

    private getConfigPath(): string {
        const homeDir = os.homedir();
        return path.join(homeDir, 'cursor-restore', 'config.json');
    }
}

